package com.qf.user.controller;

import com.qf.core.dto.PageDto;
import com.qf.core.vo.PageResult;
import com.qf.core.vo.R;
import com.qf.user.entity.User;
import com.qf.user.service.UserService;
import com.qf.user.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 管理员用户表 前端控制器
 * </p>
 *
 * @author yxh
 * @since 2023-10-29
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody User user){
        return userService.login(user);
    }

    @GetMapping("verifyToken")
    public R verifyToken(@RequestHeader("token") String token){

        User user = JWTUtil.verifyToken(token);
        if (user == null) {
            return R.fail();
        }
        return R.ok();
    }

    @PostMapping("page")
    public PageResult page(@RequestBody PageDto<User> pageDto){
        return userService.findPage(pageDto);
    }

    @PostMapping("add")
    public R add(@RequestBody User user){
        return userService.add(user);
    }

    @PostMapping("update")
    public R update(@RequestBody User user){
        return R.ok(userService.updateById(user),"修改成功");
    }

    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id){
        userService.removeById(id);
        return R.ok("删除成功");
    }







}
